﻿using InnoWeb.CEND.BL.Service;
using InnoWeb.CEND.Core.Model.General;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.Web.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class AboutPageController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<AboutPageContents> _aboutPageRepo;
        private static AboutPageContentService _aboutService;

        public AboutPageController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _aboutPageRepo = new GenericRepository<AboutPageContents>(_context);
            _aboutService = new AboutPageContentService(_unitOfWork, _aboutPageRepo);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        #region About Page Deletes

        public ActionResult Delete(int id)
        {
            return View();
        }


        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #endregion

        #region About Page Edits

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditWhyCend(int id)
        {
            var item = _aboutService.GetAllElements().First();
            return View(item);
        }


        [HttpPost]
        public ActionResult EditWhyCend(int id, AboutPageContents model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                AboutPageContents item = model;
                item.ModifiedDate = DateTime.Today;

                if (img!=null)
                {
                     fileName = Guid.NewGuid().ToString().Replace("-", "");
                     serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                     thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                     fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.WhyCendImage =  fileName + ".jpg";
                }
                else
                {
                    item.WhyCendImage = _aboutService.GetElementById(model.Id).WhyCendImage; 
                }
               
                _aboutService.UpdateElement(item);
                _aboutService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditMissionValue(int id)
        {
            var item = _aboutService.GetAllElements().First();
            return View(item);
        }


        [HttpPost]
        public ActionResult EditMissionValue(int id, AboutPageContents model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                AboutPageContents item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.MissionImage =  fileName + ".jpg";
                }
                else
                {
                    item.MissionImage = _aboutService.GetElementById(model.Id).MissionImage;
                }

                _aboutService.UpdateElement(item);
                _aboutService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditMangTeam(int id)
        {
            var item = _aboutService.GetAllElements().First();
            return View(item);
        }


        [HttpPost]
        public ActionResult EditMangTeam(int id, AboutPageContents model)
        {
            try
            {

                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                AboutPageContents item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.ManagementTeamImage =  fileName + ".jpg";
                }
                else
                {
                    item.ManagementTeamImage = _aboutService.GetElementById(model.Id).ManagementTeamImage;
                }

                _aboutService.UpdateElement(item);
                _aboutService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditCareer(int id)
        {
            var item = _aboutService.GetAllElements().First();
            return View(item);
        }


        [HttpPost]
        public ActionResult EditCareer(int id, AboutPageContents model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                AboutPageContents item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.CareerImage =  fileName + ".jpg";
                }
                else
                {
                    item.CareerImage = _aboutService.GetElementById(model.Id).CareerImage;
                }

                _aboutService.UpdateElement(item);
                _aboutService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditTerms(int id)
        {
            var item = _aboutService.GetAllElements().First();
            return View(item);
        }


        [HttpPost]
        public ActionResult EditTerms(int id, AboutPageContents model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                AboutPageContents item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.TermsAndConditionsImage =  fileName + ".jpg";
                }
                else
                {
                    item.TermsAndConditionsImage = _aboutService.GetElementById(model.Id).TermsAndConditionsImage;
                }

                _aboutService.UpdateElement(item);
                _aboutService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #endregion

        #region About Page Creates

        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #endregion
    }
}
